﻿using System;
using System.Diagnostics;
using System.Net;
using NUnit.Framework;
using Weibo4Sharp.Model;
using Weibo4Sharp.Service;

namespace Weibo4SharpTest.Service
{
    [TestFixture]
    public partial class OAuthTests : WeiboServiceTestBase
	{
        [Test]
        public void Can_get_request_token()
        {
            var service = new WeiboService(_consumerKey, _consumerSecret);

            var requestToken = service.GetRequestToken();

            Console.WriteLine("Token: {0}", requestToken.Token);
            Console.WriteLine("TokenSecret: {0}", requestToken.TokenSecret);

            AssertResultWas(service, HttpStatusCode.OK);
            Assert.NotNull(requestToken);
        }

        [Test]
        public void access_token_test2()
        {
            var service = new WeiboService(_consumerKey, _consumerSecret);

            var requestToken = service.GetRequestToken();            

            var uri = service.GetAuthorizationUri(requestToken);
            Process.Start(uri.ToString());

            var verifier = "729106";

            Console.WriteLine("foo"); // 在这里打断点，会打开浏览器。登录后会获得verifier。
            // 修改verifier的值，注意是string类型。然后继续运行。

            OAuthAccessToken accessToken = service.GetAccessToken(requestToken, verifier);          

            // 会得到Token和TokenSecret，保存下来，以后可以一直用。

            Console.WriteLine("accessToken:{0}", accessToken.Token);
            Console.WriteLine("accessTokenSecret:{0}", accessToken.TokenSecret);
            Console.WriteLine("user:{0}/{1}", accessToken.UserId, accessToken.ScreenName);
        }

        [Test]
        [Ignore("Not a sequential test.")]
        public void Can_exchange_for_access_token()
        {
            var service = new WeiboService(_consumerKey, _consumerSecret);

            var requestToken = service.GetRequestToken();

            AssertResultWas(service, HttpStatusCode.OK);
            Assert.NotNull(requestToken);

            var uri = service.GetAuthorizationUri(requestToken);
            Process.Start(uri.ToString());


            Console.WriteLine("Press the any key when you have confirmation of your code transmission.");
            var verifier = "729106"; // <-- Debugger breakpoint and edit with the actual verifier

            OAuthAccessToken accessToken = service.GetAccessToken(requestToken, verifier);
            AssertResultWas(service, HttpStatusCode.OK);
            Assert.IsNotNull(accessToken);

            Console.WriteLine("accessToken:{0}", accessToken.Token);
            Console.WriteLine("accessTokenSecret:{0}", accessToken.TokenSecret);
            Console.WriteLine("user:{0}/{1}", accessToken.UserId, accessToken.ScreenName);
        }

        [Test]
        public void Can_verify_credentials()
        {
            var service = new WeiboService(_consumerKey, _consumerSecret);
            service.AuthenticateWith(_accessToken, _accessTokenSecret);
            var user = service.VerifyCredentials();
            Assert.IsNotNull(user);
            Assert.AreNotEqual(0, user.Id);
            Console.WriteLine("userid:{0} screenName:{1}", user.Id, user.ScreenName);
        }
	}
}
